package com.ly.cloud.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.common.mybatisplus.plugins.PageInfo;
import com.ly.cloud.dto.UserDto;
import com.ly.cloud.entity.UserPo;
import com.ly.cloud.service.UserService;
import com.ly.cloud.vo.UserVo;
import com.ly.cloud.web.utils.WebResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测试demo
 */
@RestController
@RequestMapping("/user")
@Api("编写控制器对外的API说明注释")
public class UserController {

	@Resource
	private UserService userService;
	
	@ApiOperation(value = "分页查询用户列表", notes = "分页查询用户列表")
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", required = true, value = "当前页码", defaultValue = "1"),
        @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", required = true, value = "每页条数", defaultValue = "5")
	})
	@RequestMapping(value = "/queryPageList", method = RequestMethod.GET)
	public WebResponse<PageInfo<UserVo>> queryPageList(@RequestParam int pageNum, @RequestParam int pageSize, UserDto user) {
		
		try {
			Page<UserVo> page = userService.selectPage(new Page<UserVo>(pageNum, pageSize), user);
			return new WebResponse<PageInfo<UserVo>>().success(PageInfo.toPageInfo(page));
		} catch (Exception e) {
			return new WebResponse<PageInfo<UserVo>>().failure(e.getMessage());
		}
	}
	
	@ApiOperation(value = "添加新用户", notes = "添加新用户")
	@ApiImplicitParam(paramType = "body", name = "po", dataType = "UserPo", required = true, value = "用户信息JSON", defaultValue = "")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public WebResponse<Boolean> insert(@RequestBody UserDto dto) {
		try {
			boolean rs = userService.insert(dto);
			return new WebResponse<Boolean>().success(rs);
		} catch (Exception e) {
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}
	
	@ApiOperation(value = "根据编号删除用户", notes = "根据编号删除用户")
	@ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "用户账号", defaultValue = "")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	public WebResponse<Boolean> delete(@PathVariable("id") String id) {
		try {
			boolean rs = userService.delete(id);
			return new WebResponse<Boolean>().success(rs);
		} catch (Exception e) {
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}
	
	@ApiOperation(value = "根据编号批量删除用户", notes = "根据编号批量删除用户")
	@ApiImplicitParam(paramType = "body", name = "ids", dataType = "string", required = true, value = "用户账号数组", defaultValue = "")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public WebResponse<Boolean> batchDelete(@RequestBody List<String> ids) {
		try {
			boolean rs = userService.deleteBatchIds(ids);
			return new WebResponse<Boolean>().success(rs);
		} catch (Exception e) {
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}
	
	@ApiOperation(value = "更新信息", notes = "更新用户信息")
	@ApiImplicitParam(paramType = "body", name = "dto", dataType = "UserDto", required = true, value = "用户信息JSON", defaultValue = "")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public WebResponse<Boolean> update(@RequestBody UserDto dto) {
		try {
			boolean rs = userService.updateById(dto);
			return new WebResponse<Boolean>().success(rs);
		} catch (Exception e) {
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}
	
	@ApiOperation(value = "获取用户的基本信息", notes = "获取用户的基本信息")
	@ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "用户账号", defaultValue = "")
	@RequestMapping(value = "/{id}/base", method = RequestMethod.GET)
	public WebResponse<UserVo> getUserBaseInfoById(@PathVariable("id") String id) {
		try {
			UserVo user = userService.getUserBaseInfo(id);
			return new WebResponse<UserVo>().success(user);
		} catch (Exception e) {
			return new WebResponse<UserVo>().failure(e.getMessage());
		}
	}
	
	@ApiOperation(value = "获取用户的详细信息", notes = "获取用户的详细信息")
	@ApiImplicitParam(paramType = "path", name = "id", dataType = "string", required = true, value = "用户账号", defaultValue = "")
	@RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
	public WebResponse<UserPo> getUserDetailInfoById(@RequestPart("id")String id) {
		try {
			UserPo user = userService.get(id);
			return new WebResponse<UserPo>().success(user);
		} catch (Exception e) {
			return new WebResponse<UserPo>().failure(e.getMessage());
		}
	}
	
	@ApiOperation(value = "管理员修改用户密码", notes = "管理员修改用户密码")
	@ApiImplicitParams ({
		@ApiImplicitParam(paramType = "query", name = "userId", dataType = "string", required = true, value = "用户账号", defaultValue = ""),
		@ApiImplicitParam(paramType = "query", name = "password", dataType = "string", required = true, value = "用户密码", defaultValue = "")
	})
	@RequestMapping(value = "/update/password", method = RequestMethod.POST)
	public WebResponse<Boolean> updatePasswordByUserId(@RequestParam("userId")String userId, @RequestParam("password")String password) {
		try {
			boolean rs = userService.updatePasswordByUserId(userId, password);
			return new WebResponse<Boolean>().success(rs);
		} catch (Exception e) {
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}
	
	@ApiOperation(value = "个人修改密码", notes = "个人修改密码")
	@ApiImplicitParams ({
		@ApiImplicitParam(paramType = "head", name = "userId", dataType = "string", required = true, value = "用户账号", defaultValue = ""),
		@ApiImplicitParam(paramType = "query", name = "oldPassword", dataType = "string", required = true, value = "用户旧密码", defaultValue = ""),
		@ApiImplicitParam(paramType = "query", name = "newPassword", dataType = "string", required = true, value = "用户新密码", defaultValue = "")
	})
	@RequestMapping(value = "/self/update/password", method = RequestMethod.POST)
	public WebResponse<Boolean> updateUserPassword(@RequestHeader("loginUserId")String userId, @RequestParam("oldPassword")String oldPassword, @RequestParam("newPassword")String newPassword) {
		try {
			boolean rs = userService.updateUserPassword(userId, oldPassword, newPassword);
			return new WebResponse<Boolean>().success(rs);
		} catch (Exception e) {
			return new WebResponse<Boolean>().failure(e.getMessage());
		}
	}
}
  